<template>
  <view class="result-page">
    <!-- 考试情况概览 -->
    <view class="exam-summary">
      <!-- 头部标题 -->
      <view class="summary-header">
        <view class="header-left">
          <image src="/static/qs/qs_info.png" class="header-icon" mode="aspectFit"></image>
          <text class="header-title">考试情况</text>
        </view>
        <view class="header-right">
          <view class="difficulty-tag">
            <text class="difficulty-text">难度 {{ resultInfo.difficulty }}</text>
          </view>
        </view>
      </view>

      <!-- 练习详情 -->
      <view class="practice-details">
        <view class="detail-item">
          <image src="/static/qs/qs-list3.png" class="detail-icon" mode="aspectFit"></image>
          <text class="detail-label">练习题型</text>
          <text class="detail-value">{{ resultInfo.practiceType }}</text>
        </view>
        <view class="detail-item">
          <image src="/static/info/time.png" class="detail-icon" mode="aspectFit"></image>
          <text class="detail-label">交卷时间</text>
          <text class="detail-value">{{ resultInfo.submitTime }}</text>
        </view>
      </view>

      <!-- 成绩统计 -->
      <view class="performance-metrics">
        <view class="metric-item">
          <text class="metric-number total">{{ resultInfo.totalQuestions }}题</text>
          <text class="metric-label">总题数</text>
        </view>
        <view class="metric-item">
          <text class="metric-number correct">{{ resultInfo.correctCount }}题</text>
          <text class="metric-label">答对</text>
        </view>
        <view class="metric-item">
          <text class="metric-number wrong">{{ resultInfo.wrongCount }}题</text>
          <text class="metric-label">答错</text>
        </view>
        <view class="metric-item">
          <text class="metric-number unanswered">{{ getUnansweredCount() }}题</text>
          <text class="metric-label">未答</text>
        </view>
        <view class="metric-item">
          <text class="metric-number time">{{ resultInfo.totalTime }}</text>
          <text class="metric-label">总用时</text>
        </view>
      </view>
    </view>

    <!-- 答题卡详情 -->
    <view class="answer-card-section">
      <!-- 答题卡标题 -->
      <view class="card-header">
        <view class="card-title">
          <image src="/static/qs/qs-list2.png" class="card-icon" mode="aspectFit"></image>
          <text class="card-title-text">答题卡</text>
        </view>
        <view class="card-legend">
          <view class="legend-item">
            <view class="legend-circle unanswered"></view>
            <text class="legend-text">未答</text>
          </view>
          <view class="legend-item">
            <view class="legend-circle correct"></view>
            <text class="legend-text">答对</text>
          </view>
          <view class="legend-item">
            <view class="legend-circle wrong"></view>
            <text class="legend-text">答错</text>
          </view>
        </view>
      </view>

      <!-- 题目网格 -->
      <view class="question-grid">
        <view
          class="question-item"
          v-for="(answer, index) in resultInfo.answers"
          :key="index"
          :class="[getQuestionStatusClass(answer)]"
          @click="viewQuestionDetail(index)"
        >
          <text class="question-number">{{ index + 1 }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import dayjs from "dayjs";

export default {
  data() {
    return {
      practiceId: '',
      resultInfo: {
        answers: []
      }
    }
  },
  onLoad(options) {
    if (options.practiceId) {
      this.practiceId = options.practiceId;
      this.loadResultData();
    }
  },
  methods: {
    // 加载结果数据
    async loadResultData() {
      try {
        // 从API获取结果数据
        const result = await this.$api.getPracticeResult(this.practiceId);
        this.resultInfo = {
          difficulty: result.questionBank.difficulty,
          practiceType: result.questionBank.title,
          submitTime: result.end_time,
          totalQuestions: result.question_count,
          correctCount: result.correct_count,
          wrongCount: result.error_count,
          totalTime: this.formatTime(dayjs(result.end_time).diff(dayjs(result.start_time), 'seconds')),
          answers: JSON.parse(result.questions)
        }
      } catch (error) {
        console.error('加载结果数据失败:', error);
        // 使用假数据进行测试
      }
    },
    // 格式化耗时
    formatTime(time) {
      console.log('time',time)
      const minutes = Math.floor(time / 60);
      const seconds = time % 60;
      return `${minutes}分${seconds}秒`;
    },
    // 获取未答题目数量
    getUnansweredCount() {
      return this.resultInfo.answers.filter(answer => !answer.answered).length;
    },

    // 获取题目状态样式类
    getQuestionStatusClass(answer) {
      console.log('answer',answer)
      if (!answer.answered) {
        return 'unanswered';
      }
      return answer.is_correct ? 'correct' : 'wrong';
    },

    // 查看题目详情
    viewQuestionDetail(index) {
      uni.navigateTo({
        url: `/pages/question/detail?practiceId=${this.practiceId}&questionIndex=${index}`
      });
    },

    // 查看解析
    reviewQuestions() {
      uni.navigateTo({
        url: `/pages/question/review?practiceId=${this.practiceId}`
      });
    },

    // 重新练习
    retryPractice() {
      uni.showModal({
        title: '确认重新练习',
        content: '是否要重新开始练习？',
        success: (res) => {
          if (res.confirm) {
            uni.redirectTo({
              url: `/pages/question/practice?category=political_theory&title=${encodeURIComponent(this.resultInfo.practiceType)}`
            });
          }
        }
      });
    },

    // 返回首页
    goHome() {
      uni.switchTab({
        url: '/pages/index/index'
      });
    },

    // 分享结果
    shareResult() {
      uni.showActionSheet({
        itemList: ['分享到微信', '分享到朋友圈', '复制链接'],
        success: (res) => {
          switch (res.tapIndex) {
            case 0:
              this.shareToWechat();
              break;
            case 1:
              this.shareToMoments();
              break;
            case 2:
              this.copyLink();
              break;
          }
        }
      });
    },

    // 分享到微信
    shareToWechat() {
      uni.showToast({
        title: '分享功能开发中',
        icon: 'none'
      });
    },

    // 分享到朋友圈
    shareToMoments() {
      uni.showToast({
        title: '分享功能开发中',
        icon: 'none'
      });
    },

    // 复制链接
    copyLink() {
      const link = `https://example.com/practice/result/${this.practiceId}`;
      uni.setClipboardData({
        data: link,
        success: () => {
          uni.showToast({
            title: '链接已复制',
            icon: 'success'
          });
        }
      });
    }
  }
}
</script>

<style lang="scss">
.result-page {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 120rpx;
  padding-top: 24rpx;

  // 考试情况概览
  .exam-summary {
    background: #FFFFFF;
    margin: 24rpx;
    border-radius: 16rpx;
    padding: 32rpx;
	margin-top: 0;
    .summary-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 32rpx;

      .header-left {
        display: flex;
        align-items: center;

        .header-icon {
          width: 40rpx;
          height: 40rpx;
          margin-right: 16rpx;
        }

        .header-title {
          font-size: 32rpx;
          font-weight: bold;
          color: #333333;
        }
      }

      .header-right {
        display: flex;
        align-items: center;

        .difficulty-tag {
          background: #539DF3;
          padding: 8rpx 16rpx;
          border-radius: 20rpx;
          margin-right: 24rpx;

          .difficulty-text {
            font-size: 24rpx;
            color: #FFFFFF;
          }
        }

        .share-icon {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }

    .practice-details {
      margin-bottom: 32rpx;

      .detail-item {
        display: flex;
        align-items: center;
        margin-bottom: 16rpx;

        .detail-icon {
          width: 32rpx;
          height: 32rpx;
          margin-right: 16rpx;
        }

        .detail-label {
          font-size: 28rpx;
          color: #666666;
          margin-right: 16rpx;
          min-width: 120rpx;
        }

        .detail-value {
          font-size: 28rpx;
          color: #333333;
          flex: 1;
		  text-align: right;
        }
      }
    }

    .performance-metrics {
      display: flex;
      justify-content: space-between;
      padding: 24rpx;
	  background: rgba(247, 249, 250, 1);
      .metric-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        flex: 1;

        .metric-number {
          font-size: 32rpx;
          margin-bottom: 8rpx;

          &.total {
            color: #666666;
          }

          &.correct {
            color: #52c41a;
          }

          &.wrong {
            color: #ff4d4f;
          }

          &.time {
            color: #539DF3;
          }

          &.unanswered {
            color: #999999;
          }
        }

        .metric-label {
          font-size: 24rpx;
          color: #999999;
        }
      }
    }
  }

  // 答题卡详情
  .answer-card-section {
    background: #FFFFFF;
    margin: 24rpx;
    border-radius: 16rpx;
    padding: 32rpx;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 32rpx;

      .card-title {
        display: flex;
        align-items: center;

        .card-icon {
          width: 32rpx;
          height: 32rpx;
          margin-right: 16rpx;
        }

        .card-title-text {
          font-size: 32rpx;
          font-weight: bold;
          color: #333333;
        }
      }

      .card-legend {
        display: flex;
        gap: 24rpx;

        .legend-item {
          display: flex;
          align-items: center;

          .legend-circle {
            width: 24rpx;
            height: 24rpx;
            border-radius: 50%;
            margin-right: 8rpx;

            &.unanswered {
              background: #f5f5f5;
              border: 2rpx solid #e0e0e0;
            }

            &.correct {
              background: #52c41a;
            }

            &.wrong {
              background: #ff4d4f;
            }
          }

          .legend-text {
            font-size: 24rpx;
            color: #666666;
          }
        }
      }
    }

    .question-grid {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 32rpx;
      padding: 24rpx 0;
      .question-item {
        aspect-ratio: 1;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 2rpx solid transparent;

        &.unanswered {
          background: #f5f5f5;
          border-color: #e0e0e0;

          .question-number {
            color: #999999;
          }
        }

        &.correct {
          background: #52c41a;
          border-color: #52c41a;

          .question-number {
            color: #FFFFFF;
          }
        }

        &.wrong {
          background: #ff4d4f;
          border-color: #ff4d4f;

          .question-number {
            color: #FFFFFF;
          }
        }

        .question-number {
          font-size: 28rpx;
          font-weight: bold;
        }
      }
    }
  }

  // 底部操作按钮
  .bottom-actions {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #FFFFFF;
    padding: 24rpx;
    display: flex;
    gap: 16rpx;
    border-top: 1rpx solid #f0f0f0;
    z-index: 100;

    .action-btn {
      flex: 1;
      height: 88rpx;
      border-radius: 16rpx;
      border: none;
      display: flex;
      align-items: center;
      justify-content: center;

      .btn-text {
        font-size: 28rpx;
        color: #FFFFFF;
        font-weight: bold;
      }
    }

    .review-btn {
      background: #539DF3;
    }

    .retry-btn {
      background: #33B6D4;
    }

    .home-btn {
      background: #666666;
    }
  }
}
</style>
